/**
 * 
 */
package xiaojian.toolkit.bean.tlv.test;

import java.net.InetSocketAddress;

import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author hp
 *
 */
public class TlvServer {
    private static final Logger logger = LoggerFactory.getLogger(TlvServer.class);
    
    static private final int DEFAULT_SERVER_PORT = 7009;

    public static void main(String [] args) throws Exception {
        IoAcceptor acceptor = new NioSocketAcceptor();
        
        //acceptor.getFilterChain().addLast( "logger", new LoggingFilter() );
        
        acceptor.setHandler(  new IoHandler() {

            public void exceptionCaught(IoSession session, Throwable cause)
                    throws Exception {
                session.close();
            }

            public void messageReceived(IoSession session, Object message)
                    throws Exception {
            	System.out.println(message);
            	session.write(message);
            }

            public void messageSent(IoSession session, Object message)
                    throws Exception {
                
            }

            public void sessionClosed(IoSession session) throws Exception {
//                logger.debug( "sessionClosed: on client disconnected" );
//                totalConnection.getAndDecrement();
            }

            public void sessionCreated(IoSession session) throws Exception {
                
            }

            public void sessionIdle(IoSession session, IdleStatus status)
                    throws Exception {
                
            }

            public void sessionOpened(IoSession session) throws Exception {
//                int num = totalConnection.incrementAndGet();
//                logger.debug( "sessionOpened: on client connected, now connection num is :[" + num + "]");
            }} );
    
        acceptor.getSessionConfig().setReadBufferSize( 2048 );
        acceptor.getSessionConfig().setIdleTime( IdleStatus.BOTH_IDLE, 10 );
        acceptor.bind( new InetSocketAddress(DEFAULT_SERVER_PORT) );
    }
}
